<?php

/**
 * ajax返回数据
 * @param int $status       状态码
 * @param string $msg       描述
 * @param array $data       数据
 * @return string
 */
if (!method_exists('ajax_return')) {
    function ajax_return($status = 1, $msg = '', $data = [])
    {
        $res['status'] = $status;
        $res['msg']    = $msg;
        $res['data']   = $data;
        exit(json_encode($res));
    }
}

/** 时间日期转时间戳格式，精确到毫秒，
 *
 */
function get_data_format($time)
{
    list($usec, $sec) = explode(".", $time);
    $date             = strtotime($usec);
    $return_data      = str_pad($date . $sec, 13, "0", STR_PAD_RIGHT); //不足13位。右边补0
    return $return_data;
}

//去掉数字逗号
function str_d($str)
{
    $arr    = explode(',', $str);
    $newstr = '';
    foreach ($arr as $k => $v) {
        $newstr .= $v;
    }

    return $newstr;
}

//建一个对象转化为数组
function object_to_array($obj)
{
    $obj = (array) $obj;
    foreach ($obj as $k => $v) {
        if (gettype($v) == 'resource') {
            return;
        }
        if (gettype($v) == 'object' || gettype($v) == 'array') {
            $obj[$k] = (array) object_to_array($v);
        }
    }

    return $obj;
}

//生成唯一订单号(T=>微信  A=>支付宝)
function buildTradeNo(){
    $tradeNo = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    //检测是否存在
    $info = M('payorder')->where(['out_trade_no'=>$tradeNo])->find();
    (!empty($info)) && $tradeNo = $this->buildTradeNo();
    return $tradeNo;
}
